package com.hnjd.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hnjd.entity.Carts;
import com.hnjd.entity.Goods;
import com.hnjd.entity.Items;
import com.hnjd.entity.Orders;
import com.hnjd.entity.Tops;
import com.hnjd.entity.Users;
import com.hnjd.service.IindexService;
import com.hnjd.util.PageUtil;

@Controller
public class IndexController {

	@Autowired
	private IindexService service;
	
//	//首页查询
//	@RequestMapping("/index/indexList.do")
//	public String indexList(Model m) {
//		List<Goods> list = service.indexList();
//		m.addAttribute("todayList", list);
//		m.addAttribute("hotList", list);
//		return "index.jsp";
//	}
	
	//登录
	@RequestMapping("/index/login.do")
	public String login(String username, String password,Model m,HttpSession session) {
		Users user = service.login(username, password);
		if(user!=null) {
			session.setAttribute("user", user);
			return "index.jsp";
		}else {
			m.addAttribute("msg", "登陆失败，账号或密码错误");
			return "login.jsp";
		}
	}
	
	//注册
	@RequestMapping("/index/register.do")
	public String register(Users users,Model m) {
		Integer register = service.register(users);
		if(register!=null) {
			return "login.jsp";
		}else {
			m.addAttribute("msg", "注册失败，请稍后重试");
			return "register.jsp";
		}
	}
	//修改账号信息
	@RequestMapping("/index/addressUpdate.do")
	public String addressUpdate(Orders orders,HttpSession session,Model m) {
		Users user = (Users) session.getAttribute("user");
		int[] order = service.ordersById(user.getName());
		orders.setId(order[0]);
		Integer addressUpdate = service.addressUpdate(orders);
		if(addressUpdate!=null) {
			m.addAttribute("msg", "保存成功");
		}else {
			m.addAttribute("msg", "保存失败");
		}
		return "address.jsp";
	}
	
	//修改密码
	@RequestMapping("/index/passwordUpdate.do")
	public String passwordUpdate(String password,String passwordNew,HttpSession session,Model m) {
		Users user=(Users) session.getAttribute("user");
		Integer id=user.getId();
		String password1=user.getPassword();
		if(!password.equals(password1)) {
			m.addAttribute("msg", "原密码错误");
			return "password.jsp";
		}
		if(password.equals(passwordNew)) {
			m.addAttribute("msg", "两次密码一致");
			return "password.jsp";
		}
		Integer pu = service.passwordUpdate(passwordNew, id);
		if(pu!=null) {
			m.addAttribute("msg", "修改成功");
		}else {
			m.addAttribute("msg", "修改失败");
		}
		return "login.jsp";
	}
	
	//退出登录
	@RequestMapping("/index/logout.do")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		return "index.jsp";
	}
	
	//订单查询
	@RequestMapping("/index/orderList.do")
	public String orderList(Model m) {
		List<Orders> orderList = service.orderList();
		List<Items> itemList=null;
		for (Orders orders : orderList) {
			itemList = service.itemsByOrderId(orders.getId());
			for (Items items : itemList) {
				Goods Goods = service.itemsByGoodsId(items.getGood_id());
				items.setGoods(Goods);
			}
			orders.setItemList(itemList);
		}
		m.addAttribute("orderList", orderList);
		return "order.jsp";
	}
	
	//购物车查询
	@RequestMapping("/index/cart.do")
	public String cartsList(Model m,HttpSession session,Integer curPage) {
		if(curPage==null) {
			curPage=1;
		}
		int cartTotal = 0;
		Users user=(Users) session.getAttribute("user");
		if(user==null) {
			return "cart.jsp";
		}
		Goods good1=null;
		List<Carts> cartsByUserList = service.cartsByUserList(user.getId());
		for (Carts carts : cartsByUserList) {
			good1 = service.goodsById(carts.getGood_id());
			carts.setGood(good1);
			cartTotal=cartTotal+(good1.getPrice()*carts.getAmount());
		}
		PageUtil page=new PageUtil(service.cartsNum(user.getId()), 3, curPage);
		List<Carts> cartsList = service.cartsByUserId(user.getId(),page);
		Goods good=null;
		for (Carts carts : cartsList) {
			good = service.goodsById(carts.getGood_id());
			carts.setGood(good);
		}
		m.addAttribute("cartTotal", cartTotal);
		m.addAttribute("cartList", cartsList);
		m.addAttribute("page",page);
		return "cart.jsp";
	}
	
	//修改购物车商品数量
	@RequestMapping("/index/cartUpdate.do")
	public String cartUpdate(Integer id, Integer amount) {
		service.cartUpdate(id, amount);
		return "redirect:/index/cart.do";
	}
	
	//删除购物车商品
	@RequestMapping("/index/cartDelete.do")
	public String cartDelete(Integer id) {
		service.cartDelete(id);
		return "redirect:/index/cart.do";
	}
	
	@RequestMapping("/index/topGoods.do")
	public String queryAllGooods(Model m,@RequestParam(name="curPage",defaultValue = "1") Integer curPage) {
		
		
		  PageInfo pageInfo =(PageInfo) service.findGoodsTop(curPage, 5);
		  m.addAttribute("goodList", pageInfo);
		  m.addAttribute("url", "topGoods.do");
		  return "goods.jsp";
	}
	
	
	@RequestMapping("/index/salesGoods.do")
	public String querySalesGooods(Model m,@RequestParam(name="curPage",defaultValue = "1")Integer curPage) {
		  PageInfo pageInfo =(PageInfo) service.findSalesGoods(curPage,5);
		  m.addAttribute("goodList", pageInfo);
		  m.addAttribute("url", "salesGoods.do");
		  return "goods.jsp";
	}
	
	@RequestMapping("/index/topSave.do")
	public String topSave(Integer goodId,Integer type) {
		
		Tops top=new Tops();
		
		top.setGoodId(goodId);
		top.setType(type);
		System.out.println(top);
		service.topSave(top);
		
		 return "goodList.do";
	}
	 
	@RequestMapping("/index/topDelete.do")
	public String  topDelete(Integer goodId) {
		service.topDelete(goodId);
		return "goodList.do";
	}
	
	@RequestMapping("/index/addCart.do")
	@ResponseBody //返回json数据
	public Object addCart(Integer id,HttpSession session) {
		Users user=(Users) session.getAttribute("user");
		
		Map<String,Object> m = new HashMap();
		//判断商品是否存在
		if(service.cartExict(id, 1)!=0) {
			Integer data = service.addAmount(id, user.getId());
 			m.put("data", "true");
			//return data;
		}else {
			Integer data = service.addCart(id, user.getId());   
			m.put("data", "true");
			//return data;
		}
		return "1111";
	}
	
	@RequestMapping("/index/orderSave.do")
	public String orderSave(HttpSession session,Model m){
	    
	    //获取用户id
	    Users user=(Users) session.getAttribute("user");
	    //int userId=user.getId();
	    List<Orders> orderList = service.orderList();
		List<Items> itemList=null;
		for (Orders orders : orderList) {
			itemList = service.itemsByOrderId(orders.getId());
			for (Items items : itemList) {
				Goods Goods = service.itemsByGoodsId(items.getGood_id());
				items.setGoods(Goods);
			}
			orders.setItemList(itemList);
		}
		System.out.println(orderList);
		m.addAttribute("order", orderList);
	    service.generetorOrder(user);

	    // 跳转到订单支付界面
	    return "pay.jsp";
	}
}
